package com.example.zzh.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.zzh.entity.ZzhOrganization;
import com.example.zzh.entity.ZzhProductAttribute;
import com.example.zzh.entity.ZzhProductCategory;
import com.example.zzh.entity.ZzhStaff;
import com.example.zzh.service.ZzhOrganizationService;
import com.example.zzh.service.ZzhProductCategoryService;
import com.example.zzh.utils.Result;
import com.example.zzh.utils.ResultGenerator;
import com.example.zzh.utils.TimeUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 产品类型表 前端控制器
 * </p>
 *
 * @author zzh
 * @since 2023-08-12
 */
@RestController
@RequestMapping("/zzh-product-category")
public class ZzhProductCategoryController {
    @Autowired
    ZzhProductCategoryService zzhProductCategoryService;

    @GetMapping("/list")
    @ApiOperation(value = "查询所有商品类别", notes = "查询所有商品类别")
    public Result list(){
        List<ZzhProductCategory> list=zzhProductCategoryService.lambdaQuery().eq(ZzhProductCategory::getStatus,1).list();
        List<Map<String, Object>> treeData = new ArrayList<>();
        Map<Long, Map<String, Object>> map = new HashMap<>();
        for(int i=0;i<list.size();i++){
            ZzhProductCategory arrayList=list.get(i);
            Map<String,Object> node=new HashMap<>();
            node.put("id",arrayList.getId());
            node.put("label",arrayList.getName());
            // 将节点存储到map中，以便后续构建树形结构
            map.put(arrayList.getId(), node);
            if(arrayList.getParentId() != null){
                Map<String, Object> parent = map.get(arrayList.getParentId());
                if (parent != null) {
                    List<Map<String, Object>> children = (List<Map<String, Object>>) parent.get("children");
                    if (children == null) {
                        children = new ArrayList<>();
                        parent.put("children", children);
                    }
                    children.add(node);
                }
            } else {
                treeData.add(node); // 如果节点没有父节点，则将其作为根节点添加到treeData中
            }
        }
        return ResultGenerator.genSuccessResult(treeData);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增商品类别", notes = "新增商品类别")
    public Result add(@RequestBody ZzhProductCategory zzhProductCategory){
        zzhProductCategory.setCreateTime(LocalDateTime.now());
        zzhProductCategory.setUpdateTime(LocalDateTime.now());
        if(zzhProductCategoryService.save(zzhProductCategory)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"新增不成功");
        }
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改商品类别", notes = "修改商品类别")
    public Result mod(@RequestBody ZzhProductCategory zzhProductCategory){
        zzhProductCategory.setUpdateTime(LocalDateTime.now());
        if(zzhProductCategoryService.updateById(zzhProductCategory)){
            return ResultGenerator.genSuccessResult();
        }else {
            return ResultGenerator.genErrorResult(404,"修改不成功");
        }
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除商品类别", notes = "删除商品类别")
    public Result delete(@RequestParam int id){
        ZzhProductCategory zzhProductCategory=zzhProductCategoryService.getRoleById(id);
        if(!Objects.isNull(zzhProductCategory)){
            zzhProductCategory.setStatus(0);
            if(zzhProductCategoryService.updateById(zzhProductCategory)) {
                return ResultGenerator.genSuccessResult();
            }
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }else {
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }
    }

    @PostMapping("/findeByOrgNo")
    @ApiOperation(value = "模糊查询部门id", notes = "模糊查询")
    public Result findeByName(@RequestParam String name){
        List list=zzhProductCategoryService.lambdaQuery().eq(ZzhProductCategory::getName,name).list();
        return list.size()>0?ResultGenerator.genSuccessResult(list.get(0)):ResultGenerator.genFailResult("不重复");
    }

    @PostMapping("/findeById")
    @ApiOperation(value = "id查询单个信息", notes = "id查询单个信息")
    public Result findeById(@RequestParam Long id){
        ZzhProductCategory zzhProductCategory=zzhProductCategoryService.lambdaQuery().eq(ZzhProductCategory::getId,id).list().get(0);
        System.out.println("ZzhProductCategory"+zzhProductCategory.toString());
        if(zzhProductCategory !=null){
            return ResultGenerator.genSuccessResult(zzhProductCategory);
        }else {
            return ResultGenerator.genErrorResult(404,"删除不成功");
        }
    }
}

